package com.oop234.spi.impl;

import java.util.Arrays;

import com.oop234.spi.*;
import com.oop234.spi.exception.ParseException;
import com.oop234.spi.exception.ParserRegisterException;

public class Mp3SongParseServiceImpl implements SongParseService {

    public final byte[] FORMAT = "MP3".getBytes();

    public final int FORMAT_LENGTH = FORMAT.length;

    /*static {
        try {
            SongParseUtils.registerParser(new Mp3SongParseServiceImpl());
        } catch (Exception e) {
            throw new ParserRegisterException("Can't register parser!");
        }
    }*/

    @Override
    public Song parse(byte[] data) throws ParseException {
        if (!isDataCompatible(data)) {
            throw new ParseException("data format not matched.");
        }

        return new Song("《耳听爱情》", "庄心妍", "mp3", 217);
    }

    private boolean isDataCompatible(byte[] data) {
        byte[] format = Arrays.copyOfRange(data, 0, FORMAT_LENGTH);
        return Arrays.equals(format, FORMAT);
    }

}
